<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>选宿舍</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#addOneRow").click(function () {
                if ($("tr").length == 3) {
                    alert("最多三个队友!");
                } else {
                    var tempTr = $("tr:first").clone(true);
                    $("tr:last").after(tempTr);
                    $("tr:last > td > #stu_num").val("");
                    $("tr:last > td > #code").val("");
                }
            });
            $(".delOneRow").click(function () {
                if ($("tr").length < 2) {
                    $("tr:last > td > #stu_num").val("");
                    $("tr:last > td > #code").val("");
                } else {
                    if (confirm("确认删除?")) {
                        $(this).parent().parent().remove();
                    }
                }
            });
        });
    </script>
</head>
<style>
    html {
        color-scheme: light dark;
    }

    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
<body>
<iframe src="http://hub.abc.com" id="hub" width="0" height="0"></iframe>

<h1 id="selectHeader"></h1>

<div>
    请选择你想选的宿舍楼：<select id="building_id">
    <option value=1>5号楼</option>
    <option value=2>13号楼</option>
</select>
</div>

<div>
    你的学号：<input type="text" id="selfStudentNo"/>
</div>
<div>
    你的认证码：<input type="text" id="selfAuthCode"/>
</div>

<div>
    请在下方添加你本次选宿舍的队友，最多可以添加三个队友：<input type="button" id="addOneRow" value="添加队友"/>
</div>
<table id="teammates">
    <tr>
        <td>学号:</td>
        <td><input type="text" id="stu_num" name="stu_num"/></td>
        <td>认证码:</td>
        <td><input type="number" id="code" name="code"/></td>
        <td><input type="button" class="delOneRow" value="删除"/></td>
    </tr>
</table>

<div>
    <button id="btn1">确认</button>
</div>
<div>
    <button id="btn2" onclick="window.location.href='result.html'">查看结果</button>
</div>
<div>
    <button id="btn3" onclick="javascript:history.back(-1);">返回</button>
</div>


</body>

<script type="text/javascript">

    $(document).ready(function () {
        window.onload = function () {
            //var iframeWin = document.getElementById("hub").contentWindow
            // 取到token
            // let getRequest = {
            //     // 获取的方法
            //     method: 'getItem',
            //     // 获取的 key
            //     key: 'LoginToken',
            // }
            // 向 iframe “中转页面”发送消息
            // iframeWin.postMessage(getRequest, 'http://hub.abc.com')

            // 监听 iframe “中转页面”返回的消息
            //window.addEventListener('message', function (e) {
            var token = localStorage.getItem("token");

            $("#btn1").click(function () {
                var bid = document.getElementById("building_id").value;
                var teammap = {};
                var selfStudentNo = document.getElementById("selfStudentNo").value;
                var selfAuthCode = document.getElementById("selfAuthCode").value;

                if ($("tr").length > 1) {
                    for (var i = 0; i < $("tr").length; i++) {
                        var row = $("#teammates").find('tr').eq(i)
                        var num = row.find('td').eq(1).find('input').val()
                        var code = row.find('td').eq(3).find('input').val()
                        if (num in teammap) {
                            alert("队友学号不能相同！");
                            window.location.reload()
                        } else {
                            teammap[num] = code;
                        }
                    }
                    var data = {
                        "building_id": bid, "teammates_map": teammap,
                        "selfAuthCode": selfAuthCode, "selfStudentNo": selfStudentNo
                    };
                    console.log(data)
                }
                //var data = {"building_id": bid};


                $.ajax({
                    type: 'post',
                    url: 'http://localhost:8090/order/create',
                    contentType: 'application/json',

                    xhrFields: {
                        withCredentials: true // 发送Ajax时，Request header中会带上 Cookie 信息。
                    },

                    // document: Cookie.
                    // headers: {
                    //     "cookie": token
                    // },
                    dataType: 'json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        if (data.code == 200) {
                            alert('选宿舍成功');
                        } else if (data.code == 100) {
                            alert("尚未登录！");
                            window.location.href = "login.html"
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function () {
                        alert('参数错误！');
                    }
                });
            });
            $.ajax({
                type: 'post',
                url: 'http://login.abc.com/info',
                contentType: 'application/json',
                headers: {
                    "Authorization": token
                },
                dataType: 'json',
                success: function (data) {
                    if (data.status == 200) {
                        var name = data.body.stu_name;
                        $("#selectHeader").html("你好" + name + "!");
                    } else if (data.status == 301) {
                        alert("尚未登录！");
                        window.location.href = "http://login.abc.com"
                    } else {
                        alert(data.msg);
                        //window.location.href="http://abc.com"
                    }
                },
                error: function () {
                    alert('参数错误！');
                }
            });
            //})

        }
    })

</script>
</html>